// pages/postM/postM.js
var app = getApp();
const db = wx.cloud.database()
// 投递岗位
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    // 岗位管理
    postContent: [],
    touchS: [0, 0],
    touchE: [0, 0],
  },

  // ------------------------------------------------


  // 触摸开始事件
  touchStart: function (e) {
    // console.log(e.touches[0].pageX)
    let sx = e.touches[0].pageX
    let sy = e.touches[0].pageY
    this.setData({
      touchS: [sx, sy]
    });
  },
  // 触摸滑动事件
  touchMove: function (e) {
    let sx = e.touches[0].pageX;
    let sy = e.touches[0].pageY;
    this.setData({
      touchE: [sx, sy]
    });
  },
  // 触摸结束事件
  touchEnd: function (e) {
    let that = this;
    let start = this.data.touchS
    let end = this.data.touchE
    // console.log(start)
    // console.log(end)
    if (start[0] < end[0] - 50) {
      // console.log('向右滑，这里可以调用方法，及页面跳转事件')
      console.log('恢复');
      let index = e.currentTarget.dataset.index;
      that.setData({
        ['postContent[' + index + '].showDelete']: false
      })
    } else if (start[0] > end[0] + 50) {
      // console.log('向左滑，这里可以调用方法，及页面跳转事件')
      console.log('左滑');
      let index = e.currentTarget.dataset.index;
      that.setData({
        ['postContent[' + index + '].showDelete']: true
      })
    } else {
      // console.log('向上或向下滑动')
    }
  },


  getBackHr() {
    wx.navigateBack({
      delta: 1
    });
  },
  getdPerson(e) {
    console.log(e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index;
    // 获取数组下标
    app.navTmpInfo = this.data.postContent[index];

    wx.navigateTo({
      url: "/pages/dPerson/dPerson"
    })


  },

  deleteResume(arr, i, index, item_id) {
    if (arr.length === 0) {
      wx.hideLoading();
      return;
    }
    let that = this;
    // 删除简历文件
    wx.cloud.deleteFile({
      fileList: [arr[i].fileID],
      success: res => {
        console.log(res);
        // 删除简历记录
        db.collection('resume').doc(arr[i]._id).remove({
          success: res2 => {
            if (arr.length != i + 1) {
              return that.deleteResume(arr, i + 1, index, item_id);
            } else {
              let tmparr = that.data.postContent;
              tmparr.splice(index, 1);
              that.setData({
                postContent: tmparr
              });
              db.collection('job').doc(item_id).remove({
                success: () => {
                  wx.showToast({
                    title: '下架成功',
                  })
                }
              });
            }
          },
          fail: res2 => {
            console.log(res2)
          },
          complete: () => {
            wx.hideLoading();
          }
        });
      },
      fail: res => {
        console.log(res);
      }
    })

  },

  deleteJob(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    console.log(item);
    let that = this;
    wx.showModal({
      content: '您确定要下架该岗位吗？',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '下架中',
          })
          //查询所有投递记录
          db.collection('resume').where({
            job_id: item._id
          }).get({
            success: res2 => {
              if (res2.data.length != 0) {
                // 有简历投递记录
                // 递归删除简历文件和简历记录
                console.log(res2.data);
                that.deleteResume(res2.data, 0, index, item._id);
              } else {
                // 没有简历投递记录
                let tmparr = that.data.postContent;
                tmparr.splice(index, 1);
                that.setData({
                  postContent: tmparr
                });
                db.collection('job').doc(item._id).remove();

                wx.hideLoading();
                wx.showToast({
                  title: '下架成功',
                })
              }
            },
            fail: res2 => {
              console.log(res2);
              wx.hideLoading();
            }
          })
        }

      }
    })
  },


  // -----------------------------------------------------------
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    var that = this
    //查询该用户发布的岗位
    //获取数据的总个数
    let count = await db.collection('job').where({
      HR_id: app.openid,
    }).count();
    count = count.total;
    console.log(count);
    if (count == 0) {
      that.setData({
        isShow: false
      })
      console.log("无数据");
      return;
    }


    //通过for循环做多次请求，并把多次请求放到一个数组里
    let all = [];
    for (let i = 0; i < count; i += 20) {
      let list = await db.collection('job').where({
        HR_id: app.openid,
      }).skip(i).get();
      //拼接数组,concat方法
      all = all.concat(list.data);
      that.setData({
        // 直接复制整个数组
        postContent: all
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})